package com.itheima.health.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.health.dao.*;
import com.itheima.health.entity.PageResult;
import com.itheima.health.entity.QueryPageBean;
import com.itheima.health.exception.BusinessRuntimeException;
import com.itheima.health.pojo.CheckGroup;
import com.itheima.health.pojo.CheckItem;
import com.itheima.health.pojo.Setmeal;
import com.itheima.health.service.SetMealService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;


/**
 * @author zhangmeng
 * @description 套餐SEVICE实现类
 * @date 2019/9/26
 **/
@Service
@Slf4j
public class SetMealServiceImpl implements SetMealService {
    @Autowired
    private SetMealDao setMealDao;

    @Autowired
    private CheckGroupDao checkGroupDao;

    @Autowired
    private CheckItemDao checkItemDao;

    @Autowired
    private SetmealCheckGroupDao setmealCheckGroupDao;

    @Autowired
    private CheckGroupCheckItemDao checkGroupCheckItemDao;

    @Override
    public void add(Setmeal setmeal, Integer[] checkgroupIds) {
        log.info("[套餐-添加]data:{},checkgroupIds:{}",setmeal,checkgroupIds);
        // 调用DAO数据入库
        // 插入基本数据
        setMealDao.insert(setmeal);
        // 插入关联数据
        for (Integer checkgroupId : checkgroupIds) {
            setMealDao.insertSetMealAndCheckGroup(setmeal.getId(),checkgroupId);
        }

    }

    @Override
    public PageResult findPage(QueryPageBean queryPageBean) {
        //设置分页参数
        PageHelper.startPage(queryPageBean.getCurrentPage(),queryPageBean.getPageSize());
        //DAO查询数据
        Page<Setmeal> page = setMealDao.selectByCondition(queryPageBean.getQueryString());
        //封装返回值
        return new PageResult(page.getTotal(),page.getResult());
    }

    /**
     * 查询所有套餐
     * @return
     */
    @Override
    public List<Setmeal> getSetmeal() {

        return this.setMealDao.getSetmeal();
    }

    /**
     * 套餐详情查看
     */
    @Override
    public Setmeal findById(String id) {
        log.info("[根据id查询套餐详情：{}]", id);
        // 查询套餐信息
        Setmeal setmeal = setMealDao.selectById(Integer.valueOf(id));
        // 查询和套餐关联的检查组id
        List<Integer> checkGroupIdList = setmealCheckGroupDao.seleteById(Integer.valueOf(id));
        // 判断是否查询到关联的检查组
        if(checkGroupIdList == null || checkGroupIdList.size() < 1){
            throw new BusinessRuntimeException("当前套餐没有体检项目");
        }
        // 根据检查组id集合查询检查组信息
        List<CheckGroup> attention = checkGroupDao.selectByListId(checkGroupIdList);
        // 遍历检查组集合
        for (CheckGroup checkGroup : attention) {
            // 根据检查组id查询检查项id
            List<Integer> CheckItemIdList = checkGroupCheckItemDao.getCheckItemIdsByCheckGroupId(checkGroup.getId());
            // 判断是否查询到关联的检查组
            if(CheckItemIdList == null || CheckItemIdList.size() < 1){
                throw new BusinessRuntimeException("当前套餐没有体检项目");
            }
            // 根据检查项id查询检查项信息
            List<CheckItem> checkItemList = checkItemDao.selectByIds(CheckItemIdList);
            // 封装检查项集合到检查组对象中
            checkGroup.setCheckItems(checkItemList);
        }
        // 封装检查组集合到套餐对象中
        setmeal.setCheckGroups(attention);

        return setmeal;
    }

    /**
     * 修改套餐
     * @param setmeal
     * @param checkgroupIds
     */
    @Override
    public void update(Setmeal setmeal, Integer[] checkgroupIds) {
        setMealDao.updateByPrimaryKeySelective(setmeal);

        setMealDao.deleteSetMealAndCheckGroupBySetmealId(setmeal.getId());

        for (Integer checkgroupId : checkgroupIds) {
            log.info("SetmealId:{}", setmeal.getId());
            log.info("CheckgroupId:{}", checkgroupId);
            setMealDao.insertSetMealAndCheckGroup(setmeal.getId(),checkgroupId);
        }
    }

    /**
     * 根据id删除套餐
     * @param id
     * @return
     */
    @Override
    public void deleteById(int id) {
        //根据SetmealId删除套餐检查项关系表
        setMealDao.deleteSetMealAndCheckGroupBySetmealId(id);
        //根据SetmealId删除套餐
        setMealDao.deleteByPrimaryKey(id);
    }
}
